/**
 * Copyright (c) 2008, Keisuke Inoue, Syracuse Unviersity
 * All rights reserved.
 *
 */
package edu.syr.iis.web.controller;

import java.beans.PropertyEditorSupport;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.syr.iis.web.model.Conversation;
import edu.syr.iis.web.model.ConversationLabel;
import edu.syr.iis.web.model.ConversationLabelManager;
import edu.syr.iis.web.model.ConversationManager;
import edu.syr.iis.web.model.DialogueAct;
import edu.syr.iis.web.model.DialogueActLabel;
import edu.syr.iis.web.model.DialogueActLabelManager;
import edu.syr.iis.web.model.DialogueActManager;
import edu.syr.iis.web.model.DialogueActScheme;
import edu.syr.iis.web.model.DialogueActSchemeManager;
import edu.syr.iis.web.model.Message;
import edu.syr.iis.web.model.MessageManager;
import edu.syr.iis.web.model.User;
import edu.syr.iis.web.model.UserManager;
import edu.syr.iis.web.util.Key;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

/**
 * @author kinoue
 *
 */
public class AnnotateConversationController  extends SimpleFormController 
{
	MessageManager messageManager;
	UserManager userManager;
	ConversationLabelManager conversationLabelManager;
	ConversationManager conversationManager;
	
	DialogueActManager dialogueActManager;
	DialogueActLabelManager dialogueActLabelManager;
	DialogueActSchemeManager dialogueActSchemeManager;

	Conversation conversation = null;
//	Conversation nextConversation = null;
//	Conversation previousConversation = null;

	List <Message> messages = null;
	List <DialogueActLabel> dalabels = null;
	List <ConversationLabel> conversationLabels = null;
	List <DialogueActScheme> daschemes = null;

	@Override
	protected Object formBackingObject(HttpServletRequest request)
	throws Exception
	{
		if (request.getParameter(Key.CONV_ID) == null
				|| request.getParameter(Key.CONV_ID).trim().length() == 0)           
		{
			throw new Exception(Key.CONV_ID + " was not specifed.");
		}

		int conversationId = Integer.parseInt(request.getParameter(Key.CONV_ID));	
		conversation = conversationManager.getConversation(conversationId);

		if (request.getParameter(Key.DEL_DA_ID) != null
				&& request.getParameter(Key.DEL_DA_ID).trim().length() > 0) 
		{
			int daId = Integer.parseInt(request.getParameter(Key.DEL_DA_ID));   
			DialogueAct da = dialogueActManager.getDialogueAct(daId);
			if (da != null) {
				System.out.println("Deleting: " + da.toString());
				Message message = da.getMessage();
				message.deleteDialogueAct(daId);
				dialogueActManager.deleteDialogueAct(da);
			}
		}	
		
		return conversation;
	}

	@Override
	protected Map<String,Object> referenceData(HttpServletRequest request) throws Exception
	{
		HashMap<String, Object> model = new HashMap<String,Object>();

//		messages = messageManager.getMessages(conversation);
//		previousConversation = conversationManager.getPreviousConversationByConversationLabel(conversation);
//		nextConversation = conversationManager.getNextConversationByConversationLabel(conversation);

		conversationLabels = conversationLabelManager.getConversationLabels();
//		dalabels = dialogueActLabelManager.getDialogueActLabels();
//		daschemes = dialogueActSchemeManager.getDialogueActSchemes();		
		
		List<User> users = userManager.getUsers();
		
		model.put(Key.USERS, users);
//		model.put(Key.MSGS, messages);
//		model.put(Key.NEXT_CONV, nextConversation);
//		model.put(Key.PREV_CONV, previousConversation);
		model.put(Key.CLABELS, conversationLabels);
//		model.put(Key.DA_LABELS, dalabels);	
//		model.put(Key.DA_SCHEMES, daschemes);

		return model;
	}    

	public void setDialogueActSchemeManager(
			DialogueActSchemeManager dialogueActSchemeManager)
	{
		this.dialogueActSchemeManager = dialogueActSchemeManager;
	}

	@Override
	protected void initBinder(
			HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception
			{
		// binder.registerCustomEditor(ConversationLabel.class, new ConversationLabelPropertyEditor());
		binder.registerCustomEditor(User.class, new UserPropertyEditor());
			}


	@Override
	protected ModelAndView onSubmit(
			HttpServletRequest request,
			HttpServletResponse response,
			Object command,
			BindException errors)
	{
		Conversation conversation = (Conversation) command;
		Conversation nextConversation = conversationManager.getNextConversationByConversationLabel(conversation);
		
		System.out.println("Entinering AnnotateConversation:onSubmit with ");
		System.out.println(" id:       " + conversation.getConversationId());
		System.out.println(" clabelId: " + conversation.getConversationLabel().getConversationLabelId());

		conversationManager.saveConversation(conversation);

		String  mySuccessView = getSuccessView().trim() + "?conversationId=" + 
		nextConversation.getConversationId();

		return new ModelAndView(mySuccessView);
	}


	public ConversationManager getConversationManager()
	{
		return conversationManager;
	}

	public void setConversationManager(ConversationManager conversationManager)
	{
		this.conversationManager = conversationManager;
	}

	public MessageManager getMessageManager()
	{
		return messageManager;
	}

	public void setMessageManager(MessageManager messageManager)
	{
		this.messageManager = messageManager;
	}

	public ConversationLabelManager getConversationLabelManager()
	{
		return conversationLabelManager;
	}

	public void setConversationLabelManager(
			ConversationLabelManager conversationLabelManager)
	{
		this.conversationLabelManager = conversationLabelManager;
	}


	class UserPropertyEditor extends PropertyEditorSupport
	{
		@Override
		public void setAsText(String text)  throws IllegalArgumentException 
		{
			int userId = Integer.parseInt(text);
			User user = userManager.getUser(userId);

			if (user == null) {
				throw new IllegalArgumentException("No user found with id: " + userId);
			}
			setValue(user);
		}
	}

	class ConversationLabelPropertyEditor extends PropertyEditorSupport
	{
		@Override
		public void setAsText(String text)  throws IllegalArgumentException 
		{
			int clabelId = Integer.parseInt(text);
			ConversationLabel clabel = conversationLabelManager.getConversationLabel(clabelId);

			if (clabel == null) {
				throw new IllegalArgumentException("No covnersation label found with id: " + clabelId);
			}
			setValue(clabel);
		}
	}



	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	public void setDialogueActManager(DialogueActManager dialogueActManager) {
		this.dialogueActManager = dialogueActManager;
	}

	public void setDialogueActLabelManager(
			DialogueActLabelManager dialogueActLabelManager)
	{
		this.dialogueActLabelManager = dialogueActLabelManager;
	}


}
